from utils.sql_utils import SQLManager
from utils.page_utils import PageUtils
from models.note_model import NoteModel
from utils.id_utils import IdUtils
from utils.time_utils import TimeUtils
from service.follow_service import FollowService
from service.img_service import ImgService
from flask import Blueprint,request,Flask,session

class NoteService:
    def get_note_by_current_page(current,page_size):

        sql = """
            SELECT
                rnc.unique_id AS note_id,
                rnc.title AS note_title,
                rnc.content AS note_content,
                rnc.image_url AS note_image_url,
                rnc.like_count AS note_like_count,
                rnc.created_at AS note_created_at,
                rnc.modify_time AS note_modify_time,
                rnmi.location AS map_location,
                rnmi.longitude AS map_longitude,
                rnmi.latitude AS map_latitude,
                rau.unique_id AS user_id,
                rau.admin_user_name AS user_name,
                rau.avatar_url AS user_avatar_url
            FROM
                rb_note_content rnc
            JOIN
                rb_note_map_info rnmi ON rnc.map_info_id = rnmi.unique_id
            JOIN
                rb_admin_user rau ON rnc.user_id = rau.unique_id
            WHERE
                rnc.is_deleted = 0
            GROUP BY
                rnc.unique_id,
                rnc.title,
                rnc.content,
                rnc.image_url,
                rnc.like_count,
                rnc.created_at,
                rnc.modify_time,
                rnmi.location,
                rnmi.longitude,
                rnmi.latitude,
                rau.unique_id,
                rau.admin_user_name,
                rau.avatar_url
            ORDER BY
                rnc.created_at DESC;
        """
        data = SQLManager().get_list_by_page(sql,current=current,page_size=page_size) # 获取分页数据
        return data
    
    # note详情
    def get_notes_by_user_id(user_id,current):
        sql = """
            SELECT
                rnc.unique_id AS note_id,
                rnc.title AS note_title,
                rnc.content AS note_content,
                rnc.image_url AS note_image_url,
                rnc.like_count AS note_like_count,
                rnc.created_at AS note_created_at,
                rnc.modify_time AS note_modify_time,
                rnmi.location AS map_location,
                rnmi.longitude AS map_longitude,
                rnmi.latitude AS map_latitude,
                rau.unique_id AS user_id,
                rau.admin_user_name AS user_name,
                rau.avatar_url AS user_avatar_url
            FROM
                rb_note_content rnc
            JOIN
                rb_note_map_info rnmi ON rnc.map_info_id = rnmi.unique_id
            JOIN
                rb_admin_user rau ON rnc.user_id = rau.unique_id
            WHERE
                rnc.user_id = %s
                AND rnc.is_deleted = 0
            GROUP BY
                rnc.unique_id,
                rnc.title,
                rnc.content,
                rnc.image_url,
                rnc.like_count,
                rnc.created_at,
                rnc.modify_time,
                rnmi.location,
                rnmi.longitude,
                rnmi.latitude,
                rau.unique_id,
                rau.admin_user_name,
                rau.avatar_url
            ORDER BY
                rnc.created_at DESC;
            """
        
        params = (user_id,)
        note_lists = SQLManager().get_list_by_page(sql,args=params,current=int(current))
        if note_lists is None or len(note_lists) == 0:
            return False
        print('note_list',note_lists)
        for note in note_lists:
            note_id = note['note_id']
            note['slider_images'] = ImgService.query_note_slider_by_note_id(note_id,current=current,page_size=None)
        user_follow_count = FollowService.get_following_lists_count(user_id)
        note_item = note_lists[0]
        note_item['followee_count'] = user_follow_count['followee_count']

        return note_lists
    
    def update_note(request):
        sql="""
            UPDATE 
                rb_note_content 
            SET
                title = %s,
                content = %s,
                modify_time = %s
            WHERE
                unique_id = %s
            AND
                is_deleted = 0;
        """

        params = (
            request.get_json().get('note_title'),
            request.get_json().get('note_content'),
            TimeUtils.get_current_time(),
            request.get_json().get('note_id'),
        )

        return SQLManager().insert(sql,params)
    
    def delete_note(request):
        sql="""
            UPDATE 
                rb_note_content 
            SET
                is_deleted = %s
            WHERE
                unique_id = %s;
        """
        noteStatu = request.get_json().get('noteStatu')
        note_id = request.get_json().get('note_id')
        if noteStatu is None or noteStatu == '':
            noteStatu = 1
            params = (
                noteStatu,
                note_id,
            )
        else:
            params = (
                noteStatu,
                note_id,
            )

        return SQLManager().insert(sql,params)
    
    def get_notes_conunt():
        sql = """
            SELECT
                COUNT(*) AS total
            FROM
                rb_note_content rnc;
        """
        data = SQLManager().get_one(sql)  # 获取分页数据
        return data

    # 不根据状态查询全部 note
    def get_all_notes(current,page_size):

        sql = """
            SELECT
                rnc.unique_id AS note_id,
                rnc.title AS note_title,
                rnc.content AS note_content,
                rnc.image_url AS note_image_url,
                rnc.like_count AS note_like_count,
                rnc.created_at AS note_created_at,
                rnc.modify_time AS note_modify_time,
                rnc.is_deleted AS note_statu,
                rnmi.location AS map_location,
                rnmi.longitude AS map_longitude,
                rnmi.latitude AS map_latitude,
                rau.unique_id AS user_id,
                rau.admin_user_name AS user_name,
                rau.avatar_url AS user_avatar_url
            FROM
                rb_note_content rnc
            JOIN
                rb_note_map_info rnmi ON rnc.map_info_id = rnmi.unique_id
            JOIN
                rb_admin_user rau ON rnc.user_id = rau.unique_id
            GROUP BY
                rnc.unique_id,
                rnc.title,
                rnc.content,
                rnc.image_url,
                rnc.like_count,
                rnc.created_at,
                rnc.modify_time,
                rnc.is_deleted,
                rnmi.location,
                rnmi.longitude,
                rnmi.latitude,
                rau.unique_id,
                rau.admin_user_name,
                rau.avatar_url
            ORDER BY
                rnc.created_at DESC;
        """
        data = SQLManager().get_list_by_page(sql,current=current,page_size=page_size) # 获取分页数据
        return data
    # def query_note_slider_by_note_id(note_id,current,page_size):
    #     sql = """
    #         SELECT
    #             rnsi.unique_id AS slider_id,
    #             rnsi.note_id AS note_id,
    #             rnsi.slide_img_url AS image_url
    #         FROM
    #             rb_note_slide_imgs rnsi
    #         WHERE
    #            rnsi.note_id = %s
    #         AND
    #            rnsi.is_deleted = 0;
    #     """
    #     current = current or 1
    #     page_size = page_size or 5
    #     data = SQLManager().get_list_by_page(sql,args=(note_id),current=int(current))  # 获取分页数据
    #     return data
    

    
    # def get_note_slider_imag_info(note_id):
    #     sql = """
    #         SELECT
    #             rnsi.unique_id AS slider_id,
    #             rnsi.note_id AS note_id,
    #             rnsi.slide_img_url AS image_url
    #         FROM
    #             rb_note_slide_imgs rnsi
    #         WHERE
    #             rnsi.note_id = %s
    #             AND rnsi.is_deleted = 0;
    #     """
    #     data = SQLManager().get_list(sql,(note_id))  # 获取分页数据
    #     return data
    
    # def get_note_slider_imag_infos():
    #     sql = """
    #         SELECT
    #             rnsi.unique_id AS slider_id,
    #             rnsi.note_id AS note_id,
    #             rnsi.slide_img_url AS image_url
    #         FROM
    #             rb_note_slide_imgs rnsi
    #         WHERE
    #             rnsi.is_deleted = 0;
    #     """
    #     sqlManager = SQLManager()
    #     data = sqlManager.get_list(sql)  # 获取分页数据
    #     return data
    
    # # 文章展示图片插入
    # def insert_note_index_img_path(self, file, upload_path, note_id):
    #     """
    #     插入幻灯片图片路径到数据库
    #     :param file: 文件对象
    #     :param upload_path: 文件上传路径
    #     :param note_id: 笔记 ID
    #     """
    #     if not file or not upload_path or not note_id:
    #         raise ValueError("Invalid parameters provided")
        
    #     # 上传文件并获取文件 URL
    #     file_url = self.upload_file(file, upload_path)
        
    #     # 生成所需的参数
    #     unique_id = IdUtils.generate_id()
    #     current_time = TimeUtils.get_current_time_stamp()
        
    #     # SQL 查询语句
    #     sql = """
    #     INSERT INTO rb_note_slide_imgs
    #     (
    #         unique_id, note_id, slide_img_url,
    #         is_deleted, modify_time, created_at
    #     )
    #     VALUES
    #     (?, ?, ?, 0, ?, ?)
    #     """
        
    #     # 参数列表
    #     params = (unique_id, note_id, file_url, current_time, current_time)
        
    #     # 执行 SQL
    #     return SQLManager().insert(sql, params)
    
    #  # 文章展示图片插入
    # def insert_note_info(self, file, upload_path, note_id):
    #     """
    #     插入幻灯片图片路径到数据库
    #     :param file: 文件对象
    #     :param upload_path: 文件上传路径
    #     :param note_id: 笔记 ID
    #     """
    #     if not file or not upload_path or not note_id:
    #         raise ValueError("Invalid parameters provided")
        
    #     # 上传文件并获取文件 URL
    #     file_url = self.upload_file(file, upload_path)
        
    #     # 生成所需的参数
    #     unique_id = IdUtils.generate_id()
    #     current_time = TimeUtils.get_current_time_stamp()
        
    #     # SQL 查询语句
    #     sql = """
    #     INSERT INTO rb_note_slide_imgs
    #     (
    #         unique_id, note_id, slide_img_url,
    #         is_deleted, modify_time, created_at
    #     )
    #     VALUES
    #     (?, ?, ?, 0, ?, ?)
    #     """
        
    #     # 参数列表
    #     params = (unique_id, note_id, file_url, current_time, current_time)
        
    #     # 执行 SQL
    #     return SQLManager().insert(sql, params)